// 个人信息
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 用户头像
    imagePath:"",
    // 昵称
    nickName:"",
    // 姓名
    name:"",
    //手机号
    phone:"",
    // sex
    sex: "",
    nextPage:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //判断上个页面是不是问题和反馈页面
    var pages = getCurrentPages();
    var length = pages.length;
    var nextPage = pages[length - 2];
    if(nextPage.route == "help/help/help") {
      wx.showToast({
        title: '请完善个人信息',
        icon:"none"
      })
    }
    var user = wx.getStorageSync("user");
    this.setData({
      imagePath: user.head_pic,
      nickName: user.nickName,
      phone: user.phone,
      nextPage: nextPage,
      name: user.name,
      sex: user.sex
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getUserInfoFun();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 切换男女
   */
  radioChange: function(e){
    console.log(e);
    var sex = e.detail.value;
    this.setData({
      sex: sex
    })
  },
  /**
   * 选择头像
   */
  choseImage: function() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var files = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中'
        });
        wx.uploadFile({
          url: app.globalData.globalUrl + 'api/login/uploadPic',
          filePath: files[0],
          name: 'pic',
          success: function (res) {
            var data = res.data;
            if (typeof data != 'object') data = JSON.parse(data);
            if (data.code == 200) {
              var avatarUrl = app.globalData.globalUrl + data.data;
              this.setData({
                imagePath: avatarUrl
              })
            } else {
              wx.showModal({
                title: '错误',
                content: data.message
              })
            }
          }.bind(this),
          complete: function () {
            wx.hideLoading();
          }
        });
      }.bind(this)
    })
  },
  
  /**
   * 输入姓名时
   */
  inputName: function(e) {
    var name = e.detail.value;
    this.setData({
      name: name
    })
  },
  /**
   * 输入手机号
   */
  inputPhone: function (e) {
    var phone = e.detail.value;
    this.setData({
      phone: phone
    })
  },
  /**
   * 输入昵称
   */
  inputNick: function(e) {
    var nickName = e.detail.value;
    this.setData({
      nickName: nickName
    })
  },
  /**
   * 保存
   */
  save: function() {
    var user = wx.getStorageSync("user");
    var u_id = user.u_id;
    var nextPage = this.data.nextPage;
    var param = {
      u_id: u_id,
      name: this.data.name,
      nickName: this.data.nickName,
      phone: this.data.phone,
      sex: this.data.sex,
      head_pic: this.data.imagePath
    };
    user.name = this.data.name;
    user.nickName = this.data.nickName;
    user.phone = this.data.phone;
    user.sex = this.data.sex;
    user.head_pic = this.data.imagePath;
    app.getRequest("api/user/eidtUser",param,function(e){
      if(e.code == 200){
        wx.showToast({
          title: e.message
        })
        wx.setStorageSync("user", user);
        // wx.setStoragesync({
        //   key: 'user',
        //   data: user,
        // })
        wx.navigateBack()
        // if (nextPage.route == "help/help/help") {
        //   wx.navigateBack({
        //     url: '/help/help/help',
        //   })
        // }else {
        //   wx.navigateBack({
        //     url: '/person/main/main',
        //   })
        // } 
      } else {
        wx.showToast({
          title: e.message,
          icon: "none"
        })
      }
    })
   
  }
})